package com.woniuxy.project115.kaer.tenant.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.project115.kaer.commons.util.JwtUtil;
import com.woniuxy.project115.kaer.commons.util.Result;
import com.woniuxy.project115.kaer.tenant.dto.DailySaleDataDto;
import com.woniuxy.project115.kaer.tenant.dto.MonthlySaleDataDto;
import com.woniuxy.project115.kaer.tenant.entity.ServiceOrder;
import com.woniuxy.project115.kaer.tenant.form.GetServiceOrderForm;
import com.woniuxy.project115.kaer.tenant.param.GetServiceOrderParam;
import com.woniuxy.project115.kaer.tenant.service.ServiceOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuH
 * @since 2025年02月18日
 */
@RestController
@RequestMapping("/serviceOrder")
@Api(tags = "服务订单接口")
public class ServiceOrderController {
    @Resource
    private ServiceOrderService serviceOrderService;

    @PostMapping("/pay")
    @ApiOperation("支付")
    public Result Pay(String serviceOrderNum, @RequestHeader String token) throws Exception {
//        Integer userId = JwtUtil.getUserId(token);
        Integer userId = 1;
        String form = serviceOrderService.pay(serviceOrderNum, userId);
        return Result.succeed("发起支付成功", form);
    }

    @PostMapping("/noti")
    @ApiOperation("支付宝支付异步回调接口")
    public String Notify(HttpServletRequest request) throws Exception {
        System.out.println("支付宝异步回调");
        Map<String,String> params = new HashMap<String,String>();
        Map<String,String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();){
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            params.put(name, valueStr);
        }
        serviceOrderService.handleOrder(params);
        return "SUCCESS";
    }

    @PostMapping("/completeServiceOrder")
    @ApiOperation("商家完成服务订单，通知用户验收")
    public Result completeServiceOrder(String serviceOrderNum) throws Exception {
        serviceOrderService.completeServiceOrder(serviceOrderNum);
        return Result.succeed("success");
    }

    @PostMapping("/userConfirm")
    @ApiOperation("用户确认验收")
    @ApiImplicitParam(name = "serviceOrderNum", value = "服务订单号")
    public Result userConfirm(String serviceOrderNum, @RequestHeader String token) throws Exception {
        Integer userId = JwtUtil.getUserId(token);
        serviceOrderService.userConfirm(serviceOrderNum, userId);
        return Result.succeed("success");
    }

    @PostMapping("/getServiceOrder")
    @ApiOperation("获取服务订单")
    public Result<Page<ServiceOrder>> getServiceOrder(@RequestBody GetServiceOrderForm form, @RequestHeader String token) throws Exception {
        Integer storeId = JwtUtil.getStoreId(token);
        Page<ServiceOrder> page = serviceOrderService.getServiceOrder(BeanUtil.toBean(form, GetServiceOrderParam.class), storeId);
        return Result.succeed("success", page);
    }

    @PostMapping("/getDailySalesData")
    @ApiOperation("获取当天的销售数据")
    public Result<DailySaleDataDto> getDailySalesData(LocalDate date, @RequestHeader String token) throws Exception {
        Integer storeId = JwtUtil.getStoreId(token);
        DailySaleDataDto dailySalesData = serviceOrderService.getDailySalesData(date, storeId);
        return Result.succeed("success", dailySalesData);
    }

    @PostMapping("/getMonthlySalesData")
    @ApiOperation("获取当月的销售数据")
    public Result<MonthlySaleDataDto> getMonthlySalesData(LocalDate date, @RequestHeader String token) throws Exception {
        Integer storeId = JwtUtil.getStoreId(token);
        MonthlySaleDataDto monthlySalesData = serviceOrderService.getMonthlySalesData(date, storeId);
        return Result.succeed("success", monthlySalesData);
    }

    @PostMapping("/getSalesDataList")
    @ApiOperation("获取销售数据列表")
    @ApiImplicitParam(name = "number", value = "获取多少天的数据")
    public Result<DailySaleDataDto> getSalesDataList(Integer number, @RequestHeader String token) throws Exception {
        Integer storeId = JwtUtil.getStoreId(token);
        List<DailySaleDataDto> salesDataList = serviceOrderService.getSalesDataList(number, storeId);
        return Result.succeed("success", null, salesDataList);
    }
}

